<?php
/**
 * 常用辅助函数
 */

// 获取配置
function config($key = null) {
    static $config = [];
    
    if (empty($config)) {
        $files = glob(__DIR__ . '/../config/*.php');
        foreach ($files as $file) {
            $name = basename($file, '.php');
            $config[$name] = require $file;
        }
    }
    
    if ($key === null) {
        return $config;
    }
    
    return $config[$key] ?? null;
}

// JSON响应
function json_response($data, $code = 200) {
    http_response_code($code);
    header('Content-Type: application/json');
    echo json_encode($data, JSON_UNESCAPED_UNICODE);
    exit;
}

// 获取POST数据
function post($key = null, $default = null) {
    if ($key === null) {
        return $_POST;
    }
    
    return $_POST[$key] ?? $default;
}

// 获取GET数据
function get($key = null, $default = null) {
    if ($key === null) {
        return $_GET;
    }
    
    return $_GET[$key] ?? $default;
}

// base64加密
function base64_encrypt($string) {
    return base64_encode($string);
}

// base64解密
function base64_decrypt($string) {
    return base64_decode($string);
}

// 生成唯一订单号
function generate_order_number() {
    return date('YmdHis') . mt_rand(1000, 9999);
}

// 生成随机字符串
function random_string($length = 8) {
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $str = '';
    for ($i = 0; $i < $length; $i++) {
        $str .= $chars[mt_rand(0, strlen($chars) - 1)];
    }
    return $str;
}

// 检查登录状态
function check_login() {
    if (session_status() === PHP_SESSION_NONE) {
        session_start();
    }
    
    if (!isset($_SESSION['user_id'])) {
        if (strpos($_SERVER['REQUEST_URI'], '/api/') !== false) {
            json_response(['code' => 401, 'msg' => '请先登录'], 401);
        } else {
            // 根据当前路径决定跳转到哪个登录页面
            $current_path = $_SERVER['REQUEST_URI'];
            
            if (strpos($current_path, '/POS/') !== false) {
                header('Location: /POS/login.php');
            } elseif (strpos($current_path, '/chushi/') !== false) {
                header('Location: /chushi/login.php');
            } elseif (strpos($current_path, '/yh/') !== false) {
                header('Location: /yh/login.php');
            } else {
                header('Location: /login.php');
            }
            exit;
        }
    }
    return $_SESSION['user_id'];
}

// 获取当前登录用户信息
function current_user() {
    if (session_status() === PHP_SESSION_NONE) {
        session_start();
    }
    
    if (!isset($_SESSION['user_id'])) {
        return null;
    }
    
    $user_id = $_SESSION['user_id'];
    $user = Database::fetch("SELECT * FROM users WHERE id = ?", [$user_id]);
    
    return $user;
}

// 将时间格式化为中文友好格式
function friendly_date($time) {
    if (!$time) return '';
    
    $time = is_numeric($time) ? $time : strtotime($time);
    $now = time();
    $diff = $now - $time;
    
    if ($diff < 60) {
        return '刚刚';
    } elseif ($diff < 3600) {
        return floor($diff / 60) . '分钟前';
    } elseif ($diff < 86400) {
        return floor($diff / 3600) . '小时前';
    } elseif ($diff < 2592000) {
        return floor($diff / 86400) . '天前';
    } else {
        return date('Y-m-d H:i', $time);
    }
}

// 格式化金额
function format_money($money) {
    return number_format($money, 2, '.', '');
} 